import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
var _excluded = ["permissionList", "children"];

var _dec, _dec2, _class, _class2;

import React, { cloneElement } from 'react';
import PropTypes from 'prop-types';
import { Bind } from 'lodash-decorators';
import { Form } from 'antd';
import { PENDING, SUCCESS } from "../Status";
var FormItems = Form.Item;
var FormItem = (_dec = Bind(), _dec2 = Bind(), (_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(FormItem, _React$Component);

  var _super = _createSuper(FormItem);

  function FormItem() {
    var _this;

    _classCallCheck(this, FormItem);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      status: PENDING,
      controllerType: 'disabled'
    };
    return _this;
  }

  _createClass(FormItem, [{
    key: "componentDidMount",
    value: // 在 render 之前检查权限
    // eslint-disable-next-line
    function componentDidMount() {
      var permissionList = this.props.permissionList;

      if (permissionList !== undefined && Array.isArray(permissionList)) {
        this.check(this.props, this.context);
      }
    }
    /**
     * 调用 context 的 check
     * @param {object} props - 检查所需参数
     * @param {object} context - 上下文
     */

  }, {
    key: "check",
    value: function check(props, context) {
      var _props$permissionList = props.permissionList,
          permissionList = _props$permissionList === void 0 ? [] : _props$permissionList;

      if (context.permission) {
        context.permission.check({
          permissionList: permissionList
        }, this.handlePermission);
      }
    }
  }, {
    key: "handlePermission",
    value: function handlePermission(status) {
      var controllerType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'disabled';
      this.setState({
        status: status,
        controllerType: controllerType
      });
    }
  }, {
    key: "extendProps",
    value: function extendProps() {
      var _this$props = this.props,
          permissionList = _this$props.permissionList,
          children = _this$props.children,
          otherProps = _objectWithoutProperties(_this$props, _excluded);

      var _this$state = this.state,
          status = _this$state.status,
          controllerType = _this$state.controllerType;

      if (permissionList === undefined || !Array.isArray(permissionList)) {
        return /*#__PURE__*/React.createElement(FormItems, otherProps, children);
      }

      if (status === SUCCESS) {
        if (controllerType === 'disabled') {
          // 处理 field 嵌套
          if (Array.isArray(children) && React.Children.count(children) === 1) {
            return /*#__PURE__*/React.createElement(FormItems, otherProps, cloneElement(children[0], {
              disabled: true
            }));
          } else {
            return /*#__PURE__*/React.createElement(FormItems, otherProps, cloneElement(children, {
              disabled: true
            }));
          }
        } else {
          // approved=true，则controllerType=disabled则禁用，其他，则不控制
          return /*#__PURE__*/React.createElement(FormItems, otherProps, children);
        }
      } else {
        return null;
      }
    }
  }, {
    key: "render",
    value: function render() {
      return this.extendProps();
    }
  }]);

  return FormItem;
}(React.Component), _class2.contextTypes = {
  permission: PropTypes.object
}, _class2), (_applyDecoratedDescriptor(_class.prototype, "check", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "check"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handlePermission", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handlePermission"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "extendProps", [Bind], Object.getOwnPropertyDescriptor(_class.prototype, "extendProps"), _class.prototype)), _class));
export { FormItem as default };